package com.javaweb.demo.servlet;

import com.javaweb.demo.dao.UserDao;
import com.javaweb.demo.dao.UserDaolmpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


public class LoginServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        doPost(request,response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
        String name = request.getParameter("name");
        String pwd = request.getParameter("pwd");

        UserDao ud = new UserDaolmpl();

        if (ud.login(name,pwd)) {
            request.setAttribute("xiaoxi", "欢迎用户" + name);//request放置信息
            request.setAttribute("username", name);
            request.setAttribute("pwd", pwd);
            System.out.println("This is LoginSevlet pwd"+pwd);
            if (name.equals("daikunqi")) {
                request.getRequestDispatcher("/successM.jsp").forward(request,response);//转发
            } else {
                request.getRequestDispatcher("/success.jsp").forward(request,response);//转发
            }
        } else {
            request.getRequestDispatcher("/fail.jsp").forward(request, response);//重定向
        }
    }
}
